package it.unisa.dao.implementation;

import java.util.List;

import it.unisa.beans.Photo;
import it.unisa.beans.Route;
import it.unisa.dao.PhotoDao;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PhotoHibernateDao extends HibernateDaoSupport implements PhotoDao {

	

	public boolean insert(Photo photo) {
		try{
		getHibernateTemplate().save(photo);
		}
		catch(DataIntegrityViolationException e){
			return false;
		}
		return true;
	}

	public void update(Photo photo) {
		getHibernateTemplate().update(photo);
	}

	public void delete(int id) {
		Photo p=new Photo();
		p.setId(id);
		getHibernateTemplate().delete(p);
	}

	public List<Photo> find(Photo photo) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(Photo.class);
		if(photo.getId()!=0){
			criteria.add(Restrictions.eq("id", photo.getId()));
		}
		if(photo.getUrl()!=null)
			criteria.add(Restrictions.eq("url", photo.getUrl()));
		if(photo.getAccount()!=null) 
			criteria.add(Restrictions.eq("account", photo.getAccount()));
		if(photo.getPetroglyph()!=null)
			criteria.add(Restrictions.eq("petroglyph",photo.getPetroglyph()));
		return (List<Photo>)getHibernateTemplate().findByCriteria(criteria);
	}

	public List<Photo> findAllPhotos() {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find("from Photo");
	}

	public int photosCount() {
		// TODO Auto-generated method stub
		return findAllPhotos().size();
	}

}
